package com.example.myapplication.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.repository.data.File;

import java.util.List;
import java.util.function.Function;

public abstract class CommonRecyclerAdapter extends RecyclerView.Adapter<CommonRecyclerAdapter.ViewHolder>{

    LifecycleOwner lifecycleOwner;

    Context context;
    int layoutId;
    int variableId;
    List<File> list;

    Boolean clickAble;

    public void setList(List<File> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public CommonRecyclerAdapter(LifecycleOwner lifecycleOwner, Context context, int layoutId, int variableId, List<File> list) {
        this.lifecycleOwner = lifecycleOwner;
        this.context = context;
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.list = list;
    }

    public CommonRecyclerAdapter(LifecycleOwner lifecycleOwner, Context context, int layoutId, int variableId, List<File> list, Boolean clickAble) {
        this.lifecycleOwner = lifecycleOwner;
        this.context = context;
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.list = list;
        this.clickAble = clickAble;
    }

    @NonNull
    @Override
    public CommonRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext())
//                .inflate(layoutId, parent, false);
//        ListItemBinding binding = DataBindingUtil.getBinding(view);
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                layoutId, parent, false);
        binding.setLifecycleOwner(lifecycleOwner);
//        binding.setVariable(variableId,list.get())
        return new ViewHolder(binding.getRoot().getRootView(),variableId,binding);

    }

    @Override
    public void onBindViewHolder(@NonNull CommonRecyclerAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.binding.setVariable(variableId,list.get(position));
//        File fileVm = list.get(position);
//        ViewHolder viewHolder = holder;
//        viewHolder.bindItem(fileVm);

        if(clickAble){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    click(holder,position,view);
                }
            });
//            clickListener.setHolder(holder);
//            clickListener.setPosition(position);
//            holder.itemView.setOnClickListener(clickListener);
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Log.i("position",position+"");
//                }
//            });
        }
    }

    public abstract void click(CommonRecyclerAdapter.ViewHolder holder, int position, View view);

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder)
     */
    public static class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding binding;
        int variableId;


        public ViewHolder(@NonNull View itemView, int variableId, ViewDataBinding binding) {
            super(itemView);
            this.binding = binding;
            this.variableId = variableId;
        }

        void bindItem(File fileVm) {
            binding.setVariable(variableId,fileVm);
            binding.executePendingBindings();
        }

    }

//    public interface CommonRecyclerClickListener extends View.OnClickListener{
//        CommonRecyclerAdapter.ViewHolder holder;
//        int position;
//
//        @Override
//        public void onClick(View view) {
//            onClick(view,holder,position);
//        }
//
//        public  abstract void onClick(View view, CommonRecyclerAdapter.ViewHolder holder, int position);
//
//
//        public ViewHolder getHolder() {
//            return holder;
//        }
//
//        public void setHolder(ViewHolder holder) {
//            this.holder = holder;
//        }
//
//        public int getPosition() {
//            return position;
//        }
//
//        public void setPosition(int position) {
//            this.position = position;
//        }
//    }

}
